//
//  UserIDCardViewController.swift
//  EasyDana
//
//  Created by mac on 2024/10/14.
//

import UIKit
import RxSwift
import RxCocoa
import Kingfisher
class UserIDCardViewController: UIViewController {

    private let bag = DisposeBag()
    
    var naviTitle = ""
    
    var product_id: String = ""
    
    struct UserCardInfo: Codable {
        struct StuffModel: Codable {
            let angels: String
            let apoplectic: String
            let cherubs: String
            let jawed: String
        }
        let stuffs: StuffModel
    }
    
    private let data = BehaviorRelay<UserCardInfo?>.init(value: nil)
    
    @IBOutlet weak var idCardImageView: UIImageView!
    
    @IBOutlet weak var takeIDImageButton: UIButton!
    
    @IBOutlet weak var faceImageView: UIImageView!
    
    @IBOutlet weak var takeFaceButton: UIButton!
    
    @IBOutlet weak var idCardTitleLabel: UILabel!
    
    @IBOutlet weak var faceImageTitleLabel: UILabel!
    
    @IBOutlet weak var naviBar: NavigationBar!
    
    @IBAction func takeIDCardAction(_ sender: UIButton) {
        let idImage = self.data.value?.stuffs.apoplectic ?? ""
        let faceImage = self.data.value?.stuffs.jawed ?? ""
        if !idImage.hasPrefix("http") {
            //进入拍摄身份证页面
            let takeIDVC = TakeIDPhotoViewController()
            takeIDVC.product_id = self.product_id
            takeIDVC.naviTitle = self.data.value?.stuffs.angels ?? ""
            self.navigationController?.pushViewController(takeIDVC, animated: true)
        } else if !faceImage.hasPrefix("http") {
            //进入拍摄人脸页面
            let takeFaceVC = TakeFacePhotoViewController()
            takeFaceVC.product_id = self.product_id
            takeFaceVC.naviTitle = self.data.value?.stuffs.cherubs ?? ""
            self.navigationController?.pushViewController(takeFaceVC, animated: true)
        } else {
            self.navigationController?.popViewController(animated: true)
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.configView()
    }
    
    private func configView() {
        self.naviBar.title = self.naviTitle
        self.naviBar.backClicked
            .subscribe(onNext: { [weak self] _ in
                self?.navigationController?.popViewController(animated: true)
            })
            .disposed(by: self.bag)
        self.data.subscribe(onNext: { [weak self] info in
            let idImageURL = info?.stuffs.apoplectic ?? ""
            if idImageURL.count > 0 {
                self?.idCardImageView.kf.setImage(with: URL(string: idImageURL))
                self?.takeIDImageButton.isHidden = true
            } else {
                self?.idCardImageView.image = UIImage(named: "loan_id_idcard")
                self?.takeIDImageButton.isHidden = false
            }
            self?.idCardTitleLabel.text = info?.stuffs.angels
            let faceImageURL = info?.stuffs.jawed ?? ""
            if faceImageURL.count > 0 {
                self?.faceImageView.kf.setImage(with: URL(string: faceImageURL))
                self?.takeFaceButton.isHidden = true
            } else {
                self?.faceImageView.image = UIImage(named: "loan_id_pancard")
                self?.takeFaceButton.isHidden = false
            }
            self?.faceImageTitleLabel.text = info?.stuffs.cherubs
        })
        .disposed(by: self.bag)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.getData()
    }
    
    private func getData() {
        Task {
            do {
                self.showLoading(text: nil)
                let res = try await HTTPManager.default.post(path: Api.getUserCardInfo, param: ["guns": self.product_id], dataType: UserCardInfo.self)
                self.hideLoading()
                self.data.accept(res.data)
            } catch {
                self.hideLoading()
                self.show(toast: error.localizedDescription)
            }
        }
    }

}
